package com.phpxiu.app.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.phpxiu.app.adapter.CountryPickListAdapter;
import com.phpxiu.app.config.Config;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.list.Country;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * @file
 * @brief 手机登录-区号选取界面
 * @author 吴金洲
 * @note 手机登录，选取国家或地区号码
 *
 */
public class UICountryCodePicker extends UIBase implements AdapterView.OnItemClickListener,View.OnClickListener {
    public final static String SELECTED_CODE="selected_code";
    public final static String SELECTED_COUNTRY="selected_country";
    private final static Gson GSON=new Gson();
    private final static Type T=new TypeToken<ArrayList<Country>>(){}.getType();
    private List<Country> countriesList=new ArrayList<Country>();
    private List<Country> countries=new ArrayList<Country>();
    private ListView mListView;
    private CountryPickListAdapter mAdapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ui_country_code_picker);
        findViewById(R.id.top_bar_back_btn).setOnClickListener(this);
        mListView=(ListView)findViewById(R.id.region_picker_list_view);
        mListView.setOnItemClickListener(this);
        mListView.setDivider(null);
        paresJson();
        mAdapter=new CountryPickListAdapter(this,countries);
        mListView.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.top_bar_back_btn:
                onBackPressed();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(view.getTag()!=null&&view.getTag() instanceof Country){
            Country c=(Country)view.getTag();
            Intent data=new Intent();
            data.putExtra(SELECTED_CODE,c.getCode());
            data.putExtra(SELECTED_COUNTRY,c.getName());
            setResult(RESULT_OK,data);
            finish();
        }
    }

    private void paresJson(){
        String lan=getString(R.string.language);
        if("zh".equals(lan)){
            countriesList=GSON.fromJson(Config.ZH,T);
        }else if("es".equals(lan)){
            //西班牙语
            //countriesList=GSON.fromJson(Config.ES,T);
        }else{
            countriesList=GSON.fromJson(Config.EN,T);
        }
        Country group=null;
        Country lastCountry=null;
        for (Country c:countriesList){
            c.setListViewType(Country.ITEM);
            if(lastCountry!=null){
                if(!c.getGroup().equals(lastCountry.getGroup())){
                    group=new Country();
                    group.setGroup(c.getGroup());
                    group.setListViewType(Country.GROUP);
                    countries.add(group);
                }
            }else{
                group=new Country();
                group.setGroup("A");
                group.setListViewType(Country.GROUP);
                countries.add(group);
            }
            lastCountry=c;
            countries.add(c);
        }
    }
}
